//package main
//
//import (
//	"context"
//	"github.com/gin-gonic/gin"
//	"go.mongodb.org/mongo-driver/bson"
//	"go.mongodb.org/mongo-driver/mongo"
//	"go.mongodb.org/mongo-driver/mongo/options"
//	"net/http"
//)
//
//var client *mongo.Client
//
//func main() {
//	connectMongo()
//	setupWebServer()
//}
//
//func connectMongo() {
//	opts := options.Client()
//	opts.ApplyURI("mongodb://localhost:27017")
//	client, _ = mongo.Connect(context.Background(), opts)
//	//用待办事项种子数据填充数据库
//	docs := []interface{}{
//		bson.D{
//			{"id", "1"},
//			{"title", "购买杂货"},
//		},
//		bson.D{
//			{"id", "2"},
//			{"title", "安装Aspecto.io"},
//		},
//		bson.D{
//			{"id", "3"},
//			{"title", "购买dogz.io域名"},
//		},
//	}
//	client.Database("todo").Collection("todos").InsertMany(context.Background(), docs)
//}
//
//func setupWebServer() {
//	r := gin.Default()
//	r.GET("/todo", func(c *gin.Context) {
//		collection := client.Database("todo").Collection("todos")
//		//重要提示：确保将c.Request.Context()作为上下文传递，而不是c本身-TBD
//		cur, findErr := collection.Find(c.Request.Context(), bson.D{})
//		if findErr != nil {
//			c.AbortWithError(500, findErr)
//			return
//		}
//		results := make([]interface{}, 0)
//		curErr := cur.All(c, &results)
//		if curErr != nil {
//			c.AbortWithError(500, curErr)
//			return
//		}
//		c.JSON(http.StatusOK, results)
//	})
//	_ = r.Run(":8080")
//}

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/redis/go-redis/v9"
	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/propagation"
	"log"
	"net/http"
	"oteldemo/tracerprovicer"
)

var (
	cli *redis.Client
	r   *gin.Engine
)

func main() {
	//设置Exporter，将Jaeger的exporter设置为otel的Exporter
	// 返回以Jaeger为exporter的traceProvider
	tp, tpErr := tracerprovicer.JaegerTraceProvider()
	if tpErr != nil {
		log.Fatal(tpErr)
	}
	// 设置otel的provider
	otel.SetTracerProvider(tp)
	// 设置传播提取器
	otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))

	// 创建gin实例
	ginWebServer()

	//gin OpenTelemetry工具化
	//r.Use(otelgin.Middleware("todo-service"))
	r.GET("/todo", func(c *gin.Context) {
		// 创建trace实例
		tr := otel.Tracer("redis-conn")
		// 创建span实例
		spanCtx, span := tr.Start(c.Request.Context(), "get-todo")

		_ = spanCtx

		// redis opentelemetry工具化
		// Enable tracerprovicer instrumentation.
		//if err := redisotel.InstrumentTracing(cli); err != nil {
		//	panic(err)
		//}
		//spanCtx1, span1 := tr.Start(spanCtx, "redis")
		//cli.Set(spanCtx, "hello", "world", 10)
		//span1.End()
		//err := tp.Shutdown(spanCtx1)
		//if err != nil {
		//	return
		//}
		//
		////确保将c.Request.Context()作为上下文传递，而不是c本身
		//collection := cli.Get(c.Request.Context(), "hello")
		//val := collection.Val()
		//if val == "" {
		//	c.AbortWithError(500, errors.New("no found"))
		//	return
		//}

		span.End()
		tp.Shutdown(c.Request.Context())

		c.JSON(http.StatusOK, "ok")
	})

	r.Run(":8001")
}

func ginWebServer() {
	r = gin.Default()
}

func redisConn() {
	// connectMongo
	cli = redis.NewClient(&redis.Options{
		Addr: "127.0.0.1:6379",
	})
	return
}
